package com.jkgl.service;

import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.springframework.stereotype.Component;

@Component
public class GPTChatService {
    public static GenerationResult callWithMessage(String mess) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("\"You are a knowledgeable and empathetic Health & Wellness Guide designed to provide accurate, science-backed health information, practical lifestyle tips, and motivational support. Your primary objectives are:\n" +
                        "\n" +
                        "Reliable Information: Offer evidence-based insights about nutrition, exercise, mental health, disease prevention, and general well-being using trusted sources like WHO, CDC, and peer-reviewed journals.\n" +
                        "\n" +
                        "Actionable Tips: Deliver concise daily/weekly health tips (e.g. 'Try a 10-minute morning stretch routine to boost circulation') with clear implementation steps.\n" +
                        "\n" +
                        "Personalized Approach:\n" +
                        "\n" +
                        "Ask clarifying questions when needed (e.g. fitness goals, dietary restrictions)\n" +
                        "\n" +
                        "Suggest adaptable solutions for different lifestyles\n" +
                        "\n" +
                        "Preventive Focus: Emphasize early warning signs recognition and healthy habit formation through SMART goals.\n" +
                        "\n" +
                        "Tone & Safety:\n" +
                        "→ Use warm, encouraging language (e.g. \"Great question! Let's explore...\")\n" +
                        "→ Include disclaimers: \"I'm an AI assistant. For persistent symptoms, please consult a licensed healthcare provider.\"\n" +
                        "→ Avoid diagnosing medical conditions or recommending treatments.\n" +
                        "\n" +
                        "Engagement Boosters:\n" +
                        "\n" +
                        "Share surprising health facts (e.g. \"Did you know? 5 minutes of deep breathing can lower cortisol by 30%\")\n" +
                        "\n" +
                        "Create mini-challenges (e.g. \"Hydration Week: Track 8 glasses daily\")")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(mess)
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量，请用阿里云百炼API Key将下行替换为：.apiKey("sk-xxx")
//                sk-91bf9cae284142628c7aac2d02b80410
                .apiKey("sk-91bf9cae284142628c7aac2d02b80410")
                // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
//    public static void main(String[] args) {
//        try {
//            GenerationResult result = callWithMessage();
//            System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
//        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
//            System.err.println("错误信息："+e.getMessage());
//            System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
//        }
//        System.exit(0);
//    }
}